/**
 * 
 */
package pl.net.beercrm.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import pl.net.beercrm.model.Purchase;

/**
 * @author Rafi
 *
 */
@Repository
@Transactional(propagation = Propagation.MANDATORY)
public class PurchaseDao implements GenericDao<Purchase, Long> {

	@Autowired
	SessionFactory sessionFactory;
	
	private Class<Purchase> getEntityClass() {
		return Purchase.class;
	}

	/* (non-Javadoc)
	 * @see pl.net.beercrm.dao.GenericDao#create(java.lang.Object)
	 */
	@Override
	public void create(Purchase object) {
		sessionFactory.getCurrentSession().persist(object);
	}

	/* (non-Javadoc)
	 * @see pl.net.beercrm.dao.GenericDao#update(java.lang.Object)
	 */
	@Override
	public void update(Purchase object) {
		sessionFactory.getCurrentSession().update(object);
	}

	/* (non-Javadoc)
	 * @see pl.net.beercrm.dao.GenericDao#delete(java.lang.Object)
	 */
	@Override
	public void delete(Purchase persistentObject) {
		sessionFactory.getCurrentSession().delete(persistentObject);
	}

	/* (non-Javadoc)
	 * @see pl.net.beercrm.dao.GenericDao#findById(java.io.Serializable)
	 */
	@Override
	public Purchase findById(Long id) {
		return (Purchase) sessionFactory.getCurrentSession().get(
				this.getEntityClass(), id);
	}

	/* (non-Javadoc)
	 * @see pl.net.beercrm.dao.GenericDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Purchase> findAll() {
		return (List<Purchase>) sessionFactory.getCurrentSession()
				.createCriteria(this.getEntityClass()).list();
	}

	/* (non-Javadoc)
	 * @see pl.net.beercrm.dao.GenericDao#findAllByProperty(java.lang.String, java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Purchase> findAllByProperty(String propertyName, Object value) {
		return (List<Purchase>) sessionFactory.getCurrentSession()
				.createCriteria(this.getEntityClass())
				.add(Restrictions.eq(propertyName, value)).list();
	}

}
